﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WAV.Utility
{
    public class WAV_INFO
    {
        public string Riff { get; set; } //4
        public Int32 RiffSize { get; set; }//4 byte
        public string WavID { get; set; }//4
        public string FmtID { get; set; }//4
        public Int32 NotDefintion { get; set; }//4 过滤字节
        public Int16 WaveType { get; set; }//2 1表示PCM编码
        public Int16 Channel { get; set; }//2 通道数 1单声道   2立体声
        public Int32 Sample { get; set; }//4 采样率
        public Int32 Bps { get; set; }//4  比特率
        public Int16 BlockAdjust { get; set; }//数据的调整数（按字节计算）2
        public Int16 BitNum { get; set; }//pwm分辨率2

        public Int16 Unknow { get; set; }//2
        public string UnknowHeader { get; set; }//4
        public Int32 UnknowLength { get; set; }//4 可选端长度 小段模式
        public byte[] UnknowInfo { get; set; }

        public string DataID { get; set; }//4
        public Int32 DataLength { get; set; }//4
        public byte[] Data { get; set; }

        public WAV_INFO ReadWavByte(byte[] cdata)
        {
            WAV_INFO wavinfo = new WAV_INFO();
            var encoding = Encoding.Default;
            wavinfo.Riff = encoding.GetString(cdata, 0, 4);//4
            wavinfo.RiffSize = BitConverter.ToInt32(cdata, 4);//8
            wavinfo.WavID = encoding.GetString(cdata, 8, 4);//11
            wavinfo.FmtID = encoding.GetString(cdata, 12, 4);//16
            wavinfo.NotDefintion = BitConverter.ToInt32(cdata, 16);//20
            wavinfo.WaveType = BitConverter.ToInt16(cdata, 20);//22
            wavinfo.Channel = BitConverter.ToInt16(cdata, 22);//24
            wavinfo.Sample = BitConverter.ToInt32(cdata, 24);//28
            wavinfo.Bps = BitConverter.ToInt32(cdata, 28);//32
            wavinfo.BlockAdjust = BitConverter.ToInt16(cdata, 28);//34
            wavinfo.BitNum = BitConverter.ToInt16(cdata, 34);//36
            if (wavinfo.NotDefintion == 0x10)  //没有附加信息
            {
                wavinfo.DataID = encoding.GetString(cdata, 36, 4);//40
                wavinfo.DataLength = BitConverter.ToInt32(cdata, 40);//44
                List<byte> by = new List<byte>();
                for (int i = 44; i < cdata.Length; i++)
                {
                    by.Add(cdata[i]);
                }
                wavinfo.Data = by.ToArray();

            }
            else //有附加信息
            {
                wavinfo.Unknow = BitConverter.ToInt16(cdata, 36);//38 
                wavinfo.UnknowHeader = encoding.GetString(cdata, 38, 4);//44
                IEnumerable<char> cr =
                   from w in encoding.GetChars(cdata, 44, 4)
                   orderby w ascending
                   select w;
                wavinfo.UnknowLength = cr.ToArray()[0] << 24 | cr.ToArray()[1] << 16 | cr.ToArray()[2] << 8 | cr.ToArray()[3];//48              
                List<byte> by = new List<byte>();
                for (int i = 48; i < wavinfo.UnknowLength + 48; i++)
                {
                    by.Add(cdata[i]);
                }
                wavinfo.UnknowInfo = by.ToArray();// wavinfo.UnknowLength+48

                wavinfo.DataID = encoding.GetString(cdata, wavinfo.UnknowLength + 48, 4);//wavinfo.UnknowLength+48+4
                wavinfo.DataLength = BitConverter.ToInt32(cdata, wavinfo.UnknowLength + 52);//wavinfo.UnknowLength + 56
                List<byte> by1 = new List<byte>();
                for (int i = wavinfo.UnknowLength + 56; i < cdata.Length; i++)
                {
                    by1.Add(cdata[i]);
                }
                wavinfo.Data = by.ToArray();

            }
            return wavinfo;
        }
    }
}
